package com.ruoyi.web.controller.security;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.security.convert.SecurityConvert;
import com.ruoyi.security.domain.Security;
import com.ruoyi.security.dto.SecurityDto;
import com.ruoyi.security.dto.SyncSecurityListDto;
import com.ruoyi.security.service.ISecurityService;
import com.ruoyi.security.vo.SecurityVo;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author yeoman
 * @date 2025/4/30 16:14
 * @description 证券
 */
@Controller
@RequestMapping("/security")
public class SecurityController  extends BaseController {

    private String prefix = "security";

    @Resource
    private ISecurityService securityService;

    @GetMapping()
    public String security()
    {
        return prefix + "/security";
    }

    /**
     * 证券列表同步页面
     */
    @GetMapping("/syncList")
    public String syncList()
    {
        return prefix + "/sync-list";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SecurityDto dto) {
        startPage();
        List<Security> list = securityService.list(dto);
        List<SecurityVo> voList = SecurityConvert.INSTANCE.toSecurityVoList(list);
        return getDataTable(voList, list);
    }

    /**
     * 证券列表同步
     */
    @Log(title = "证券列表同步", businessType = BusinessType.INSERT)
    @PostMapping("/syncList")
    @ResponseBody
    public AjaxResult syncList(@Validated SyncSecurityListDto dto) {
        securityService.syncList(dto);
        return toAjax(true);
    }

    /**
     * 单个证券同步
     */
    @Log(title = "证券同步", businessType = BusinessType.INSERT)
    @PostMapping("/sync")
    @ResponseBody
    public AjaxResult sync(@RequestParam @NotBlank(message = "证券代码不能为空") String symbol) {
        try {
            logger.info("开始同步证券数据，证券代码：{}", symbol);
            
            securityService.sync(symbol);
            
            logger.info("证券同步完成，证券代码：{}", symbol);
            return AjaxResult.success("证券同步成功");
        } catch (Exception e) {
            logger.error("证券同步失败，证券代码：{}", symbol, e);
            return AjaxResult.error("证券同步失败：" + e.getMessage());
        }
    }

    /**
     * 获取所有证券列表（用于下拉选择）
     */
    @GetMapping("/securities")
    @ResponseBody
    public AjaxResult getAllSecurities() {
        try {
            logger.info("获取所有证券列表");
            List<Security> securities = securityService.list(new SecurityDto());
            List<SecurityVo> securityVos = SecurityConvert.INSTANCE.toSecurityVoList(securities);
            return AjaxResult.success("获取证券列表成功", securityVos);
        } catch (Exception e) {
            logger.error("获取证券列表失败", e);
            return AjaxResult.error("获取证券列表失败：" + e.getMessage());
        }
    }

}
